home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
3_11.lha
/
3_11
/
3_11a_b.c
next >
Wrap
Text File
|
1993-08-08
|
578b
|
33 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ Convert a string of digits into an integer,
/ checking for overflow
/ Version 2
include <ctype.h>
include <limits.h>
include <error.h>
nt atoi(const char *s)
for (int i = 0; isdigit(*s); s++)
{
if (i > INT_MAX / 10)
break;
i *= 10;
int n = *s - '0';
if (i > INT_MAX - n)
break;
i += n;
}
if (isdigit(*s))
error("integer overflow");
else if (*s)
error("malformed integer");
return i;